<?php
namespace app\index\controller;
use app\BaseController;
use app\index\model\Collect as ModelCollect;
use think\facade\Db;
use think\facade\Session;

class Collect extends Base
{
    public function doCollect($art_id)
    {
        $this->logs('收藏文章');
        $isHas = ModelCollect::where(['article_id' => $art_id,'user_id' => $this->user['user_id']])->findOrEmpty()->isEmpty();
        if(!$isHas){
            return error('无需重复操作');
        }else{
            $res = ModelCollect::create(['article_id' => $art_id , 'user_id' => $this->user['user_id']]);
            if($res) return success();
            return error('收藏失败');
        }
    }
    public function unCollect($art_id)
    {
        $this->logs('取消收藏');
        $isHas =  ModelCollect::where(['article_id' => $art_id,'user_id' => $this->user['user_id']])->findOrEmpty()->isEmpty();
        if(!$isHas){
            $res = ModelCollect::where('article_id' , $art_id)->delete();
            if($res) return success();
            return error();
        }
        return error('异常');
    }
}
